﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy4_9
{
    public partial class Form1 : Form
    {
        Form2 f2 = new Form2(); // 实例化子窗体
        string nowPath = "";    // 记录当前文件路径

        public Form1()
        {
            InitializeComponent();
        }

        // “新建”菜单/工具栏按钮点击事件
        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            f2
.ShowDialog(); // 显示Form2并等待用户返回
            if (f2.listBox1.Items.Count > 0)
            {
                nowPath
= ""; // 新建文件，清空路径标记
                txtConfig
.Clear();
                for (int i = 0; i < f2.listBox1.Items.Count; i++)
                {
                    txtConfig
.Text += f2.listBox1.Items[i].ToString() + "\r\n";
                }
            }
        }

        // “打开”菜单/工具栏按钮点击事件
        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd
.Filter = "文本文件|*.txt|所有文件|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                nowPath
= ofd.FileName;
                txtConfig
.Text = System.IO.File.ReadAllText(nowPath);
                this.Text = "计算机配置 - " + nowPath;
            }
        }

        // “保存”菜单/工具栏按钮点击事件
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(nowPath))
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd
.Filter = "文本文件|*.txt|所有文件|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    nowPath
= sfd.FileName;
                    System
.IO.File.WriteAllText(nowPath, txtConfig.Text);
                    this.Text = "计算机配置 - " + nowPath;
                }
            }
            else
            {
                System
.IO.File.WriteAllText(nowPath, txtConfig.Text);
            }
        }

        // “字体”菜单/快捷菜单点击事件
        private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fd = new FontDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtConfig
.Font = fd.Font;
            }
        }

        // “背景色”菜单/快捷菜单点击事件
        private void 背景色CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            if (cd.ShowDialog() == DialogResult.OK)
            {
                txtConfig
.BackColor = cd.Color;
            }
        }

        // “退出”菜单点击事件
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application
.Exit();
        }

        // “关于”菜单/快捷菜单点击事件
        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox
.Show("计算机配置清单管理程序\n版本：1.0", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
